/**
 * @file qq_recent_contact.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_QQ != 0
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void qq_recent_msg_list_time_start_update(lv_obj_t * obj);
static void qq_recent_contact_list_add_btn(lv_obj_t * list, watch_qq_user_info_t * user);
static void qq_remove_contact_list_node(char * openid);
/**********************
 *  STATIC VARIABLES
 **********************/
static char * my_cookie = NULL;
static lv_ll_t qq_contact_ll;
static uint8_t msg_sync_flag = 0;
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void qq_recent_contact_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED == event) {
        qq_friend_btn_ext_t * btn_ext = lv_obj_get_ext_attr(btn);
        watch_qq_user_info_t * info = &btn_ext->info;
        lv_qq_obj_ext_t * ext = qq_get_ext();
        lv_obj_set_hidden(ext->leds_content, true);
        if(lv_obj_count_children(btn) == 3)
            lv_obj_del(lv_obj_get_child(btn, NULL));
        qq_remove_contact_list_node(btn_ext->info.openid);
        qq_create_chat(ext->recentmsg, info);
    } else if(LV_EVENT_DELETE == event) {
        printf("%s:clean up btn ext\n", __FUNCTION__);
        qq_friend_btn_ext_t * btn_ext = lv_obj_get_ext_attr(btn);
        if(btn_ext->info.headurl) lv_mem_free(btn_ext->info.headurl);
        if(btn_ext->info.nickname) lv_mem_free(btn_ext->info.nickname);
        if(btn_ext->info.remark) lv_mem_free(btn_ext->info.remark);
    }
}

void qq_free_my_cookie(void)
{
    if(my_cookie) {
        lv_mem_free(my_cookie);
        my_cookie = NULL;
    }
}

void qq_get_recent_contact(void)
{
#if QQ_UI_TEST == 0
    watch_qq_get_recent_contact_req_t * get_recent_contactreq = lv_mem_alloc(sizeof(watch_qq_get_recent_contact_req_t));
    memset(get_recent_contactreq, 0, sizeof(watch_qq_get_recent_contact_req_t));
    get_recent_contactreq->contact_cnt = MMI_QQ_GET_RECENT_MSG_CNT;
    get_recent_contactreq->contact_time = MMI_QQ_GET_RECENT_MSG_TIME;
    watch_qq_get_recent_contact_req(get_recent_contactreq);
#endif
}

void qq_get_msg()
{
#if QQ_UI_TEST == 0
    watch_qq_get_msg_req_t * get_msgreq = lv_mem_alloc(sizeof(watch_qq_get_msg_req_t));
    memset(get_msgreq, 0, sizeof(watch_qq_get_msg_req_t));
    get_msgreq->cookie = my_cookie;
    get_msgreq->sync_flag =  msg_sync_flag;
    get_msgreq->get_ramblemsg = 0;
    watch_qq_get_msg_req(get_msgreq);
    my_cookie = NULL;
#endif
}

void qq_init_contact_list(void)
{
    memset(&qq_contact_ll, 0, sizeof(lv_ll_t));
    _lv_ll_init(&qq_contact_ll, sizeof(qq_contact_info_t));
}

void qq_add_contact_list(char * openid)
{
    printf("%s: openid = %s\n", __FUNCTION__, openid);
    bool find = false;
    qq_contact_info_t * node = _lv_ll_get_head(&qq_contact_ll);
    while(node) {
        if(strncmp(node->openid, openid, 64) == 0) {
            find = true;
            break;
        }
        node = _lv_ll_get_next(&qq_contact_ll, node);
    }

    if(!find) {
        qq_contact_info_t * new_node = _lv_ll_ins_tail(&qq_contact_ll);
        if(new_node) {
            memset(new_node, 0, sizeof(qq_contact_info_t));
            strncpy(new_node->openid, openid, 64);
        }
    }
}

void qq_clear_contact_list(void)
{
    _lv_ll_clear(&qq_contact_ll);
}

static void qq_remove_contact_list_node(char * openid)
{
    qq_contact_info_t * node = _lv_ll_get_head(&qq_contact_ll);
    while(node) {
        if(strncmp(node->openid, openid, 64) == 0) {
            _lv_ll_remove(&qq_contact_ll, node);
            lv_mem_free(node);
            break;
        }
        node = _lv_ll_get_next(&qq_contact_ll, node);
    }
}

void qq_show_chat_new_message(watch_qq_get_msg_cnf_t * get_msgcnf)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    if(get_msgcnf->result == WATCH_QQ_OK) {
        my_cookie = lv_strdup(get_msgcnf->cookie);
        if(get_msgcnf->sync_flag == 1) {
            msg_sync_flag = get_msgcnf->sync_flag;
            qq_get_msg();
        } else {
            msg_sync_flag = 0;
        }
        if(get_msgcnf->user_infocnt > 0) {
            watch_qq_user_info_t * user_node = NULL;
            user_node = _lv_ll_get_head(&get_msgcnf->user_infos_ll);
            while(user_node) {
                if(ext->msg_list) {
                    watch_qq_user_info_t * chat_info = lv_watch_get_free_ptr(ext->name);
                    printf("%s: user openid = %s, chat openid = %s\n", __FUNCTION__, user_node->openid, chat_info->openid);
                    if(strncmp(user_node->openid, chat_info->openid, 64) == 0) {
                        qq_add_msg_into_list(&get_msgcnf->msg_infos_ll);
                        user_node = _lv_ll_get_next(&get_msgcnf->user_infos_ll, user_node);
                        continue;
                    }
                }
                qq_add_contact_list(user_node->openid);
                user_node = _lv_ll_get_next(&get_msgcnf->user_infos_ll, user_node);
            }
        }
    }
    if(ext->msg_list == NULL)
        qq_get_recent_contact();
}

void qq_create_recent_contacts_list(lv_ll_t * recent_info_ll)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    if(ext->recent_list) {
        lv_list_clean(ext->recent_list);
    } else {
        lv_obj_clean(ext->recentmsg);
        lv_obj_t * list = qq_create_list(ext->recentmsg);
        lv_obj_t * tileview = lv_obj_get_parent(ext->recentmsg);
        lv_tileview_add_element(tileview, list);
        lv_tileview_add_element(tileview, lv_page_get_scrl(list));
        ext->recent_list = list;
    }

    watch_qq_user_info_t * node = (watch_qq_user_info_t *)_lv_ll_get_head(recent_info_ll);
    while(node) {
        qq_recent_contact_list_add_btn(ext->recent_list, node);
        node = (watch_qq_user_info_t *)_lv_ll_get_next(recent_info_ll, node);
    }
    qq_recent_msg_list_time_start_update(ext->recent_list);
}

void qq_no_msg_recently(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    if(ext->recent_list) return;

    lv_obj_t * label = lv_obj_get_child(ext->recentmsg, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_NO_MSG_RECENTLY);
    lv_obj_realign(label);
}

void qq_delete_recent_contact(lv_obj_t * list, char * openid)
{
    if(list == NULL) return;
    lv_obj_t * btn = lv_list_get_next_btn(list, NULL);
    qq_friend_btn_ext_t * btn_ext = NULL;
    while(btn) {
        btn_ext = lv_obj_get_ext_attr(btn);
        if(strncmp(btn_ext->info.openid, openid, 64) == 0) {
            lv_list_remove(list, lv_list_get_btn_index(list, btn));
            break;
        }
        btn = lv_list_get_next_btn(list, btn);
    }
    if(lv_list_get_size(list) == 0) {
        lv_obj_del(list);
        lv_qq_obj_ext_t * ext = qq_get_ext();
        ext->recent_list = NULL;

        lv_obj_t * img = lv_img_create(ext->recentmsg, NULL);
        lv_img_set_src(img, ICON_QQ_MSG);
        lv_obj_align(img, NULL, LV_ALIGN_CENTER, 0, -LV_VER_RES / 10);

        lv_obj_t * label = lv_label_create(ext->recentmsg, NULL);
        lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_NO_MSG_RECENTLY);
        lv_obj_align(label, img, LV_ALIGN_OUT_BOTTOM_MID, 0, LV_VER_RES / 8);
        lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    }
}

/**********************
 *   STATIC FUNCTIONS
***********************/
static void qq_recent_contact_list_add_btn(lv_obj_t * list, watch_qq_user_info_t * user)
{
    lv_obj_t * btn;
    qq_friend_btn_ext_t * btn_ext = NULL;
    char time_str[16];
    lv_obj_t * newmsg_tip;
    char * path = NULL;

    memset(time_str, 0, 16);
    qq_get_timestr_from_timestamp(user->contact_time, time_str, 16);
    if(user->remark)
        btn = qq_list_add(list, ICON_QQ_DEF_HEAD, user->remark, (void *)time_str, LIST_TYPE_RECENT_CONTACTS);
    else
        btn = qq_list_add(list, ICON_QQ_DEF_HEAD, user->nickname, (void *)time_str, LIST_TYPE_RECENT_CONTACTS);
    path = watch_qq_get_friend_head_path(user->openid);
    if(path != NULL) {
        lv_obj_t * head_img = lv_obj_get_child_back(btn, NULL);
        lv_img_set_src(head_img, path);
        lv_mem_free(path);
    }
    btn_ext = qq_btn_allocate_ext(btn, user);
    LV_ASSERT_MEM(btn_ext);
    lv_obj_set_event_cb(btn, qq_recent_contact_btn_event_cb);

    qq_contact_info_t * node = _lv_ll_get_head(&qq_contact_ll);
    while(node) {
        if(strncmp(node->openid, user->openid, 64) == 0) {
            printf("%s: unread name = %s\n", __FUNCTION__, user->nickname);
            newmsg_tip = lv_img_create(btn, NULL);
            lv_img_set_src(newmsg_tip, ICON_QQ_UNREAD_MSG);
            lv_obj_align(newmsg_tip, btn, LV_ALIGN_IN_RIGHT_MID, -lv_obj_get_width(newmsg_tip), 0);
        }
        node = _lv_ll_get_next(&qq_contact_ll, node);
    }
}

static void qq_recent_msg_list_time_update(lv_anim_t * anim)
{
    char time_str[16];
    lv_obj_t * list = anim->var;
    lv_obj_t * scrl = lv_page_get_scrl(list);
    lv_obj_t * list_obj = lv_obj_get_child(scrl, NULL);
    while(list_obj) {
        qq_friend_btn_ext_t * btn_ext = lv_obj_get_ext_attr(list_obj);
        memset(time_str, 0, 16);
        qq_get_timestr_from_timestamp(btn_ext->info.contact_time, time_str, 16);
        lv_obj_t * cont;
        if(lv_obj_count_children(list_obj) > 2) {
            cont = lv_obj_get_child(list_obj, lv_obj_get_child(list_obj, NULL));
        } else
            cont = lv_obj_get_child(list_obj, NULL);
        lv_obj_t * time_label = lv_obj_get_child(cont, NULL);
        lv_label_set_text(time_label, time_str);
        list_obj = lv_obj_get_child(scrl, list_obj);
    }
}

static void qq_recent_msg_list_time_start_update(lv_obj_t * obj)
{
    lv_anim_t a;
    lv_anim_path_t path;

    lv_anim_path_init(&path);
    lv_anim_path_set_cb(&path, lv_anim_path_step);
    lv_anim_init(&a);
    lv_anim_set_path(&a, &path);
    lv_anim_set_var(&a, obj);
    lv_anim_set_time(&a, 0);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 60000);
    lv_anim_set_start_cb(&a, qq_recent_msg_list_time_update);
    lv_anim_start(&a);
}

#endif /*USE_LV_WATCH_QQ*/
